﻿package test{
	/**
	 * @author Marcelo Volmaro
	 */
	import extremefx.storage.XmlLoader;	
	import extremefx.storage.LoadQeue;	
	import extremefx.DateTime;
	import extremefx.StringBuilder;
	import extremefx.events.EventArgs;
	import extremefx.i18n.Culture;
	import extremefx.i18n.Resources;
	import extremefx.i18n.XString;
	import extremefx.i18n.locales.ES_AR;
	import extremefx.i18n.locales.GU_IN;
	import extremefx.i18n.locales.RU_RU;
	import extremefx.i18n.locales.SA_IN;
	import extremefx.i18n.locales.ZH_TW;
	
	import test.AnyRadix;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.text.TextField;
	import flash.text.TextFormat;	

	[Frame(factoryClass="extremefx.Preloader")];
	public final class CultureDemo extends Sprite {
		
		private var _tf:TextField;
		private var _sb:StringBuilder;
		private var _loader:LoadQeue;

		public function CultureDemo() {}

		private function showTests(cult:Culture):void{
			var date:DateTime = DateTime.now; 
			_sb.appendFormatLine("Lang id: {0}, two letter code: {1}, english name: {2}",cult.id, cult.name, cult.englishName);

			Culture.currentCulture = cult;

			_sb.appendFormatLine("Date: {0:F}",date);
			_sb.appendFormatLine("Number: {0:n} / {1:n}",1123123120.23, -1123123120.23);
			_sb.appendFormatLine("Currency: {0:c} / {1:c}",323423410.23, -323423410.23);
			_sb.appendFormatLine("Percentage: {0:p} / {1:p}",234234210.23, -234234210.23);
			_sb.appendLine();
		}
		
		private function convertToAnyRadix(argToConvert:Object, formatStr:String ):void{
			var provider:AnyRadix = new AnyRadix();
        	var messageStr:String = XString.format( "{0:{0}}", formatStr );
        	
        	_sb.appendFormat("{0,18}  {1,-6} :", argToConvert, formatStr);
        	
			// Convert the specified argument using the specified format.
			try {
				_sb.appendLine(XString.format(provider, messageStr, argToConvert));
				
			} catch(ex:Error) {
				_sb.appendLine("Error:" + ex);
			}
        }

		public function main():void{
			var a:ZH_TW, b:SA_IN, c:RU_RU, d:ES_AR, e:GU_IN;
			a = null;//this is not needed... only for me to not have errors in FDT :)
			b = null;
			c = null;
			d = null;
			e = null;

			var tf:TextField = _tf = new TextField();
			tf.defaultTextFormat = new TextFormat("arial unicode ms", 14, 0);
			
			var sb:StringBuilder = _sb = new StringBuilder();
			
			_loader = new LoadQeue();
			var xmlL:XmlLoader = _loader.addLoader(new XmlLoader("xliff.xml")) as XmlLoader;
			xmlL.onComplete.add(function(pSender:XmlLoader, pArgs:EventArgs):void{
				Resources.add(pSender.xml);
			});
		
			_loader.onQeueComplete.add(_onLoad);
			
			stage.addEventListener(Event.RESIZE, resizeStage);
			
			addChild(tf);

			tf.x = 0;
			tf.y = 0;
			tf.width = stage.stageWidth;
			tf.height = stage.stageHeight;
			tf.border = true;
	
			showTests(new Culture("en-US"));
			showTests(new Culture("zh-TW"));
			showTests(new Culture("sa-IN"));
			showTests(new Culture("ru-RU"));
			showTests(new Culture("es-AR"));
			showTests(new Culture("gu-IN"));
			
			var twoToThe32:Number = 4294967296;
        	var fifteenNines:Number = 999999999999999;
        	
        	sb.appendLine("This example of the IFormatProvider interface \n" +
            "and the IFormatProvider.GetFormat( Type ) method " +
            "\ngenerates the following output.\n" );
            
        	sb.appendFormatLine("{0,18} Format Result", "Number");
        	sb.appendFormatLine("{0,18} ------ ------", "------");

        	// These are valid conversions.
	        convertToAnyRadix( twoToThe32, "RA2" );
	        convertToAnyRadix( twoToThe32, "RA5" );
	        convertToAnyRadix( twoToThe32, "RA16" );
	        convertToAnyRadix( twoToThe32, "RA23" );
	        convertToAnyRadix( twoToThe32, "RA36" );
	        convertToAnyRadix( fifteenNines, "RA2" );
	        convertToAnyRadix( fifteenNines, "RA3" );
	        convertToAnyRadix( fifteenNines, "RA8" );
	        convertToAnyRadix( fifteenNines, "RA11" );
	        convertToAnyRadix( fifteenNines, "RA16" );
	        convertToAnyRadix( fifteenNines, "RA23" );
	        convertToAnyRadix( fifteenNines, "RA36" );
	        convertToAnyRadix( fifteenNines, "E16" );
	        convertToAnyRadix( fifteenNines, "" );
	
	        // These are error conditions.
	        convertToAnyRadix( fifteenNines, "RA37" );
	        convertToAnyRadix( "ABCDEFGHIJKLM", "RA16" );
	        sb.appendLine();
			
			tf.text = sb.toString();
		}

		private function resizeStage(event:Event):void {
			_tf.width = stage.stageWidth;
			_tf.height = stage.stageHeight;
		}
		
		private function _onLoad(pSender:Object, pArgs:EventArgs):void{
			_tf.appendText('Format Number Choice\n');
			
			for (var i:uint = 0; i<3; i++){
				_tf.appendText(XString.format(Resources.get("test3", i), i) + '\n');
			}
			
			var a:DisplayObject = addChild(DisplayObject(Resources.get("image")));
			a.x = stage.stageWidth - a.width;
			a.y = 10;
			
			a = addChild(DisplayObject(Resources.get("image2")));
			a.x = stage.stageWidth - a.width;
			a.y = 80;
		}
	}
}
